
package guoan.repository;

import java.util.List;

import guoan.domain.RandomKey;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface RandomKeyRepository extends PagingAndSortingRepository<RandomKey, Long>, JpaSpecificationExecutor<RandomKey> {
    List<RandomKey> findByTrainIdAndCoachType(Long trainId, Long coachType);

    int countByKeyAndIsUse(String random, long l);

    List<RandomKey> findByTrainIdAndCoachTypeAndIsUse(Long trainId, Long coachType, long l);

    Long countByTrainIdAndCoachTypeAndIsUse(Long trainId, Long coachType, long l);

    RandomKey findByKeyAndIsUse(String key, long l);

    List<RandomKey> findByStudentId(Long studentId);

    RandomKey findByStudentIdAndCoachType(Long studentId, long coachType);

    int countByCoachTypeAndStudentId(Long coachType, Long id);

    @Modifying
    @Query(value = "UPDATE random_key SET student_id = null WHERE student_id = :studentId AND coach_type = :coachType", nativeQuery = true)
    int updateByStudentIdAndCoachType(@Param("studentId") Long studentId, @Param("coachType") Long coachType);

    RandomKey findByKey(String key);
}
